package com.raising.modules.sys.controller;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.*;

import com.raising.framework.controller.BaseController;
import com.raising.framework.entity.ResultVo;
import com.raising.framework.interceptor.Page;
import com.raising.modules.sys.utils.DictUtils;
import com.raising.StaticParam;

import com.raising.utils.JDateUtils;

import com.raising.modules.sys.entity.Elog;
import com.raising.modules.sys.service.ElogService;

/**
 * 错误日志 控制器
 * @author gaoyuan
 * @createTime 2018-06-05 12:57:07
 */
@Controller
@RequestMapping("${adminPath}/sys/elog")
public class ElogController extends BaseController {

    @Autowired
    private ElogService elogService;

    /**
     * 跳转至列表页面
     * @author gaoyuan
     * @createTime 2018-06-05 12:57:07
     */
    @RequiresPermissions("sys:elog:view")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list() {
        return "sys/elog/list";
    }

    /**
     * 加载列表分页数据
     * @author gaoyuan
     * @createTime 2018-06-05 12:57:07
     */
    @RequiresPermissions("sys:elog:view")
    @RequestMapping(value = "/getPage.json", method = RequestMethod.POST)
    public @ResponseBody ResultVo getPage(Elog elog,Page<Elog> page,String searchKey) throws Exception{
        if(searchKey!=null && !"".equals(searchKey)){
            elog.setUsername(searchKey);
        }
        page.setEntity(elog);
        page.setOrderBy(" a.id desc ");
        return ResultVo.success().data(elogService.getPage(page));
    }

    /**
     * 跳转新增页面
     * @author gaoyuan
     * @createTime 2018-06-05 12:57:07
     */
    @RequiresPermissions("sys:elog:create")
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String create(Model model) {
        model.addAttribute("elog", new Elog());
        // 是否可用
        model.addAttribute("yn", DictUtils.getDictList("DIM_YN"));
        model.addAttribute("showDateDefault", JDateUtils.getDate());
        return "sys/elog/add";
    }

    /**
     * 执行新增操作
     * @author gaoyuan
     * @createTime 2018-06-05 12:57:07
     */
    @RequiresPermissions("sys:elog:create")
    @RequestMapping(value = "/create.json", method = RequestMethod.POST)
    @ResponseBody
    public ResultVo createJson(Elog elog) throws Exception{
        elog.setStatus(StaticParam.VALID_STRING);
        return elogService.insert(elog);
    }

    /**
     * 跳转更新页面
     * @author gaoyuan
     * @createTime 2018-06-05 12:57:07
     */
    @RequiresPermissions("sys:elog:update")
    @RequestMapping(value = "/update", method = RequestMethod.GET)
    public String update(String id, Model model) throws Exception{
        model.addAttribute("elog", elogService.get(id));
        // 是否可用
        model.addAttribute("yn", DictUtils.getDictList("DIM_YN"));
        return "sys/elog/edit";
    }

    /**
     * 执行更新操作
     * @author gaoyuan
     * @createTime 2018-06-05 12:57:07
     */
    @RequiresPermissions("sys:elog:update")
    @RequestMapping(value = "/update.json", method = RequestMethod.POST)
    @ResponseBody
    public ResultVo updateJson(Elog elog) throws Exception{
        elog.setStatus(StaticParam.VALID_STRING);
        return elogService.update(elog);
    }

    /**
     * 执行删除操作
     * @author gaoyuan
     * @createTime 2018-06-05 12:57:07
     */
    @RequiresPermissions("sys:elog:delete")
    @RequestMapping(value = "/delete.json", method = RequestMethod.POST)
    @ResponseBody
    public ResultVo deleteJson(String id) throws Exception{
        return elogService.delete(id);
    }

    /**
     * 跳转查看页面
     * @author gaoyuan
     * @createTime 2018-06-05 12:57:07
     */
    @RequiresPermissions("sys:elog:view")
    @RequestMapping(value = "/view", method = RequestMethod.GET)
    public String view(String id, Model model) throws Exception{
        model.addAttribute("elog", elogService.get(id));
        // 是否可用
        model.addAttribute("yn", DictUtils.getDictList("DIM_YN"));
        return "sys/elog/view";
    }


}
